<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
//公用功能类库
use other\FanJianConvert;

use app\admin\model\UserModel;
use app\admin\model\CommonModel;
class User extends Common{
    public function __construct()
    {
        parent::__construct();                        //继承父类的构造方法
    } 
/**
用户管理
*/
	/**
    * 获取用户列表
    * @author 星  2018/11/6
    */
    public function userList()
    {
        $translate=array(
            'czxk'=>'roleid_name',
            'zh'=>'username',
            'ygmc'=>'name',
            'dhhm'=>'phone',
            'Email'=>'email',
            'sd'=>'state',
            'zdjd'=>'automatic',
            'bzsm'=>'remarks',
            'zhdrsj'=>'login_time',
            'zhdrip'=>'login_ip',
            'lzsj'=>'leave_date',
        );
        $common = new CommonModel();
        //获取方法路径
        $url = $common->get_type();
        $url='/'.$url;
        //获取用户对于本菜单中列表的设置 
        $order=$common->setOrder($url);
        $postData = Request::instance()->param();
    	$user = new UserModel();
    	$users = $user->userList($postData);
        $page = $users->render();
        $user = $users->toArray()['data'];
        if(request()->isAjax()){
            $data['user'] = $user;
            $data['page'] = $page; // 分页的按钮  
            $data['order'] = $order;
            return json($data);
        }
        $this->assign("url",$url);
        $this->assign("order",$order);
        $this->assign("page",$page);
        $this->assign("translate",$translate);
    	$this->assign("user",$user);
    	return $this->fetch("/user/user");
    }

    /**
    * 用户详细信息
    * @author 星  2018/9/14
    */
   	public function userShow()
	{
		$id = Request::instance()->param('id');				 //获取传过来的id
		$user = new UserModel();
        $userShow = $user->userShow($id);                    //利用构造方法实例化且调用userShow方法
        $roleid = $user->getRoleid();
        $Branch = $user->getBranch();
        $this->assign('roleid',$roleid);
        $this->assign('userShow',$userShow);
        $this->assign('branch',$Branch);
        return $this->fetch("/user/user-show");
	}

	/**
    * 添加用户
    * @author 星  2018/9/14
    */
    public function userAdd()
    {
    	$data = Request::instance()->param();                 //获取传过来的数据 
    	$user = new UserModel();
        $result = $user->userAdd($data);      	 			  //利用构造方法实例化且调用userAdd添加方法
        return json($result);
    }

    /**
    * 修改用户
    * @author 星  2018/9/14
    */
   	public function userEdit()
	{
		$data = Request::instance()->param();				  //获取传过来的数据 
    	$user = new UserModel();
        $result = $user->userEdit($data,$data['id']);		  //利用构造方法实例化且调用userEdit修改方法
        return json($result);
	}

	/**
    * 逻辑删除用户
    * @author 星  2018/9/14
    */
	public function userIsDelete()
	{
		$id = Request::instance()->param('id');          	  //获取传过来的id  
    	$user = new UserModel();
        $user->userIsDelete($id);    	 			  //利用构造方法实例化且调用staffManageDelete删除方法
        return redirect("/admin/user/userList");
	}

    /**
    * 逻辑删除用户
    * @author 星  2018/9/14
    */
    public function userAllIsDelete()
    {
        $data = Request::instance()->param();               //获取传过来的id
        $user = new UserModel();
        $user->userAllIsDelete($data);                   //利用构造方法实例化且调用staffManageDelete删除方法
        return json(['code'=>1,'msg'=>'删除成功']);
    }

    public function userHotel()
    {
        return $this->fetch("/user/user-hotel");
    }
/**
角色管理
*/
    /**
    * 角色列表
    * @author 星  2018/9/14
    */
    public function roleList()
    {
        $translate=array(
            'bh'=>'roleid_sn',
            'mc'=>'roleid_name',
            'bz'=>'remarks',
            'tjsj'=>'add_time',
        );
        $postData = Request::instance()->param();
        $common = new CommonModel();
        //获取方法路径
        $url = $common->get_type();
        $url='/'.$url;

        // //获取用户对于本菜单中列表的设置 
        $order=$common->setOrder($url);
        $user = new UserModel();
        $roleLists = $user->roleList($postData);    //利用构造方法实例化且调用hotelRoomList方法
        $roleList = $roleLists->toArray()['data'];
        $page = $roleLists->render();
        if(request()->isAjax()){
            $data['role'] = $roleList;
            $data['page'] = $page; // 分页的按钮  
            $data['order'] = $order;
            return json($data);
        }
        $this->assign('url',$url);
        $this->assign('roleList',$roleList);
        $this->assign('translate',$translate);
        $this->assign('order',$order);
        $this->assign('page',$page);
        return $this->fetch("/user/role");
    }

    /**
    * 角色详细信息
    * @author 星  2018/9/14
    */
    public function roleShow()
    {
        $user = new UserModel();
        $id = Request::instance()->param('id');              //获取传过来的id
        $roleShow = $user->roleShow($id);                    //利用构造方法实例化且调用roleShow方法
        // print_r($roleShow);die;
        $this->assign('roleShow',$roleShow);                 //赋值
        return $this->fetch("/user/role-show");
    }

    /**
    * 添加角色
    * @author 星  2018/9/14
    */
    public function roleAdd()
    {
        $user = new UserModel();
        $data = Request::instance()->param();                //获取传过来的数据 
        $result = $user->roleAdd($data);                    //利用构造方法实例化且调用roleAdd添加方法
        return json($result);
    }

    /**
    * 角色信息修改
    * @author 星  2018/9/14
    */
    public function roleEdit()
    {
        $user = new UserModel();
        $data = Request::instance()->param();                   //获取传过来的数据 
        $result = $user->roleEdit($data,$data['id']);          //利用构造方法实例化且调用roleEdit修改方法
        return json($result);
    }

    /**
    * 逻辑删除角色
    * @author 星  2018/9/14
    */
    public function roleIsDelete()
    {
        $user = new UserModel();
        $id = Request::instance()->param('id');                //获取传过来的id  
        $user->roleIsDelete($id);                                //利用构造方法实例化且调用roleDelete删除方法
        return redirect("/admin/user/roleList");
    }

    /**
    * 批量逻辑删除角色
    * @author 星  2018/11/7
    */
    public function roleAllIsDelete()
    {
        $user = new UserModel();
        $data = Request::instance()->param();                //获取传过来的id  
        $user->roleAllIsDelete($data);                                //利用构造方法实例化且调用roleDelete删除方法
        return json(['code'=>1,'msg'=>'删除成功']);
    }

    /**
    * 物理删除角色
    * @author 星  2018/9/14
    */
    public function roleDelete()
    {
        $user = new UserModel();
        $id = Request::instance()->param('id');              //获取传过来的id  
        $user->roleDelete($id);                          //利用构造方法实例化且调用roleRealDelete删除方法
        return $this->success('删除成功！');
    }

    /**
    * 根据id获取下一级数据
    * @author 星  2018/12/4
    */
    public function getMenuData()
    {
        $user = new UserModel();
        $id = Request::instance()->param('id');              //获取传过来的id  
        $result = $user->getMenuData($id);
        return json($result);
    }

   /**
    * 后台菜单列表显示
    * @author 星  2018/9/17
    */
    public function menuList()
    {   
        // $str = Request::instance()->param('str');
        // print_r($str);die;
        $user = new UserModel();
        $menu = $user->menuListss();
        return json($menu);
    }

    /**
    * 左侧菜单列表显示
    * @author 星  2018/9/17
    */
    public function searchMenu()
    {   
        $str = Request::instance()->param('str');
        $user = new UserModel();
        $menu = $user->searchMenu($str);
        return json($menu);
    }

    /**
    * 后台菜单列表显示
    * @author 星  2018/9/17
    */
    public function menuLists()
    {
    	$translate=array(
            'bh'=>'menu_id',
            'mc'=>'menu_name',
            'fxgl'=>'fxgl',
            'yc'=>'menu_hidden',
            'lj'=>'menu_url',
            'bz'=>'menu_remark',
            'px'=>'menu_order',
        );

        $common = new CommonModel();
        //获取方法路径
        $url = $common->get_type();
        $url='/'.$url;
        // //获取用户对于本菜单中列表的设置 
        $order=$common->setOrder($url);
        $postData = Request::instance()->param();  // 请求参数
        $user = new UserModel();
        $menuLists = $user->menuLists($postData);
        $page = $menuLists->render();
        $menu = $menuLists->toArray()['data'];
        $menu = $user->hasSonData($menu);
        if(empty($menu)) {
            if(!empty($postData['parent'])) {
                $menu = $postData['parent'];
            } else {
                if(session('menu_parent')) {
                    $menu = session('menu_parent');
                } else {
                    $menu = 0;
                }
            }
        }

        if(request()->isAjax()){
            $data['menu'] = $menu;
            $data['page'] = $page; // 分页的按钮 
            $data['order'] = $order;                         
            return json($data); 
        }
        $this->assign("order",$order);
        $this->assign("url",$url);
        $this->assign("translate",$translate);
        $this->assign("menu",$menu);
        $this->assign("page",$page);
        return $this->fetch("/user/menu");
    }

    /**
    * 获取下一级菜单
    * @author 星  2018/11/8
    */
    public function getSonData()
    {
    	$common = new CommonModel();
        //获取方法路径
        $url="/admin/user/menuLists";
        // //获取用户对于本菜单中列表的设置 
        $order=$common->setOrder($url);
        $data = Request::instance()->param();
        $user = new UserModel();
        $menu = $user->getSonData($data);
        $page = $menu->render();
        $menu = $menu->toArray()['data'];
        $menu['menu'] = $user->hasSonData($menu);
        if(!$menu['menu']) {
        	$menu['menu'] = $data['id'];
        }
        $menu['page'] = $page; // 分页的按钮
        $menu['order'] = $order; 
        $menu['session'] = session("menu"."$data[id]");
        return json($menu);
    }

    /**
    * 获取上一级菜单
    * @author 星  2018/11/8
    */
    public function getParentData()
    {
    	$common = new CommonModel();
        //获取方法路径
        $url="/admin/user/menuLists";
        // //获取用户对于本菜单中列表的设置 
        $order=$common->setOrder($url);
        $data = Request::instance()->param();
        $user = new UserModel();
        $menu = $user->getParentData($data);
        $page = $menu->render();
        $menu = $menu->toArray()['data'];
        $menu['menu'] = $user->hasSonData($menu);
        if(!$menu['menu']) {
        	$menu['menu'] = $data['id'];
        }
        $menu['page'] = $page; // 分页的按钮
        $menu['order'] = $order; 
        $parentid = $user->getParentId($data['id']);
        $menu['session'] = session("menu"."$parentid");
        return json($menu);
    }
    
    /**
    * 菜单详细信息
    * @author 星  2018/11/9
    */
    public function menuShow()
    {
        $id = Request::instance()->param('id');
        $parent = Request::instance()->param('parent');
        $user = new UserModel();
        $menuShow = $user->menuShow($id);
        $this->assign("menuShow",$menuShow);
        $this->assign("parent",$parent);
        return $this->fetch("/user/menu-show");
    }

    /**
    * 添加菜单
    * @author 星  2018/9/17
    */
    public function menuAdd()
    {
        $user = new UserModel();
        $data = Request::instance()->param();                //获取传过来的数据 
        $result = $user->menuAdd($data);                     //利用构造方法实例化且调用menuAdd添加方法
        return json($result);
    }

    /**
    * 菜单信息修改
    * @author 星  2018/9/17
    */
    public function menuEdit()
    {
        $user = new UserModel();
        $data = Request::instance()->param();                 //获取传过来的数据 
        $result = $user->menuEdit($data,$data['id']);         //利用构造方法实例化且调用menuEdit修改方法
        return json($result);
    }

    /**
    * 逻辑删除菜单
    * @author 星  2018/9/17
    */
    public function menuIsDelete()
    {
        $user = new UserModel();
        $id = Request::instance()->param('id');               //获取传过来的id  
        $parent = $user->menuIsDelete($id);                   //利用构造方法实例化且调用menuIsDelete删除方法
        return redirect("/admin/user/menuLists?parent=".$parent);
    }

    /**
    * 批量逻辑删除菜单
    * @author 星  2018/9/17
    */
    public function menuAllIsDelete()
    {
        $user = new UserModel();
        $data = Request::instance()->param();               //获取传过来的id  
        $user->menuAllIsDelete($data);                   //利用构造方法实例化且调用menuIsDelete删除方法
        return json(['code'=>1,'msg'=>'批量删除成功']);    }

    /**
    * 物理删除菜单
    * @author 星  2018/9/17
    */
    public function menuDelete()
    {
        $user = new UserModel();
        $id = Request::instance()->param('id');               //获取传过来的id  
        $result = $user->menuDelete($id);                     //利用构造方法实例化且调用menuDelete删除方法
        if($result) {                                         //判断是否成功
            return $this->success('删除成功！');
        }
        return $this->error('验证未通过!');
    }

    /**
     * 基本设置
     * @author 星  2018/11/12
     */
    public function config()
    {
        $user = new UserModel();
        $config = $user->config();
        $this->assign("config",$config);
        return $this->fetch("/user/config");
    }

    /**
     * 基本设置添加修改
     * @author 星  2018/11/12
     */
    public function configEdit()
    {
        $user = new UserModel();
        $data = Request::instance()->param(); 
        $config = $user->configEdit($data);
        return json($config);
    }

    /**
     * 功能管理
     * @author 星  2018/11/13
     */
    public function functionList()
    {
        $id = Request::instance()->param('id');
        $user = new UserModel();
        $functionList = $user->functionList($id);
        $this->assign("id",$id);
        $this->assign("functionList",$functionList);
        return $this->fetch("/user/function");
    }

    /**
     * 功能管理详细信息
     * @author 星  2018/11/13
     */
    public function functionShow()
    {
        $id = Request::instance()->param('id');
        $fid = Request::instance()->param('fid');
        $user = new UserModel();
        $functionShow = $user->functionShow($fid);
        $this->assign("id",$id);
        $this->assign("functionShow",$functionShow);
        return $this->fetch("/user/function-show");
    }

    /**
     * 功能管理添加
     * @author 星  2018/11/13
     */
    public function functionAdd()
    {
        $data = Request::instance()->param();
        $user = new UserModel();
        $result = $user->functionAdd($data);
        return json($result);
    }

    /**
     * 功能管理修改
     * @author 星  2018/11/13
     */
    public function functionEdit()
    {
        $data = Request::instance()->param();
        $user = new UserModel();
        $result = $user->functionEdit($data);
        return json($result);
    }

    /**
     * 功能管理删除
     * @author 星  2018/11/13
     */
    public function functionIsDelete()
    {
        $fid = Request::instance()->param('fid');
        $id = Request::instance()->param('id');
        $user = new UserModel();
        $result = $user->functionIsDelete($fid);
        return redirect("/admin/user/functionList?id=".$id);
    }

    /**
     * 功能管理批量删除
     * @author 星  2018/11/13
     */
    public function functionAllIsDelete()
    {
        $data = Request::instance()->param();
        $user = new UserModel();
        $result = $user->functionAllIsDelete($data);
        return json(['code'=>1,'msg'=>'删除成功']);
    }

/**
部门管理
*/
    /**
     * 部门列表
     * @author lin  2019/05/09
     */
    public function branchList()
    {
        $translate=array(
            'bh'=>'branch_sn',
            'mc'=>'branch_name',
            'bz'=>'remarks',
            'tjsj'=>'add_time',
        );
        $postData = Request::instance()->param();
        $common = new CommonModel();
        //获取方法路径
        $url = $common->get_type();
        $url='/'.$url;

        // //获取用户对于本菜单中列表的设置
        $order=$common->setOrder($url);
        $user = new UserModel();
        $branchLists = $user->branchList($postData);    //利用构造方法实例化且调用hotelRoomList方法
        $branchList = $branchLists->toArray()['data'];
        $page = $branchLists->render();
        if(request()->isAjax()){
            $data['branch'] = $branchList;
            $data['page'] = $page; // 分页的按钮  
            $data['order'] = $order;
            return json($data);
        }
        $this->assign('url',$url);
        $this->assign('branchList',$branchList);
        $this->assign('translate',$translate);
        $this->assign('order',$order);
        $this->assign('page',$page);
        return $this->fetch("/user/branch");
    }

    /**
     * 部门管理详细信息
     * @author lin  2019/05/09
     */
    public function branchShow(){
        $branch = new UserModel();
        $id = Request::instance()->param('id');              //获取传过来的id
        $branchShow = $branch->branchShow($id);                    //利用构造方法实例化且调用roleShow方法
        // print_r($roleShow);die;
        $this->assign('branchShow',$branchShow);                 //赋值
        return $this->fetch("/user/branch-show");
    }

    /**
     * 部门管理添加
     * @author lin  2019/05/09
     */
    public function branchAdd(){
        $data = Request::instance()->param();
        $user = new UserModel();
        $result = $user->branchAdd($data);
        return json($result);
    }

    /**
     * 部门管理修改
     * @author lin  2019/05/09
     */
    public function branchEdit()
    {
        $data = Request::instance()->post();
        $id = Request::instance()->get('id');
        $user = new UserModel();
        $result = $user->branchEdit($data,$id);
        return json($result);
    }
    /**
     * 部门管理删除
     * @author lin  2019/05/09
     */
    public function branchIsDelete(){
        $id = Request::instance()->param('id');
        $user = new UserModel();
        $result = $user->branchIsDelete($id);
        return redirect("/admin/user/branchList");
    }

    /**
     * 后台登陆账户修改密码
     * @author  jiajing  2019-10-11
     */
    public function changePassword(){
        $data = Request::instance()->param();
        $passwords = new UserModel();
        $result = $passwords->changePassword($data);
        return $result;
    }
}
